package com.hs.jpademo1.service.impl;

import com.hs.jpademo1.dao.StudentDao;
import com.hs.jpademo1.entity.Student;
import com.hs.jpademo1.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.persistence.criteria.Predicate;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName StudentServiceImpl
 * @Description TODO
 */
@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentDao mapper;

    @Override
    public Page<Student> findByPage(Integer pageNum, Integer pageSize) {
        if(pageNum < 0) {
            pageNum = 0;
        }
        PageRequest request = PageRequest.of(pageNum, pageSize);
        return mapper.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();
            predicates.add(criteriaBuilder.equal(root.get("state"), 0));
            return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }, request);
    }

    @Override
    public Student findById(Integer id) {
        return mapper.findById(id).get();
    }

    @Override
    public int updateStudent(Student student) {
        student.setState(0);
        Student save = mapper.save(student);
        if (!student.equals(save)) {
            throw  new RuntimeException("更新失败");
        }
        return 1;
    }

    @Override
    public int addStudent(Student student) {
        Student save = mapper.save(student);
        if(!save.equals(student)) {
            throw new RuntimeException("新增失败");
        }
        return 1;
    }

    @Override
    public int removeStudent(Integer id) {
        int i = mapper.removeById(id);
        if(i < 1) {
            throw new RuntimeException("删除失败");
        }
        return 0;
    }

    @Override
    public Page<Student> findByCondition(Integer pageNum, Integer pageSize,Student student) {
        if(pageNum < 0) {
            pageNum = 0;
        }
        PageRequest request = PageRequest.of(pageNum, pageSize);
        return mapper.findAll((root, criteriaQuery, criteriaBuilder) -> {
            List<Predicate> predicates = new ArrayList<Predicate>();

            if (student.getName() != null && !"".equals(student.getName())) {
                predicates.add(criteriaBuilder.like(root.get("name"), "%"+student.getName()+"%"));
            }

            if (student.getSex() != null && !"".equals(student.getSex())) {
                predicates.add(criteriaBuilder.equal(root.get("sex"), student.getSex()));
            }

            if (student.getAddress() != null && !"".equals(student.getAddress())) {
                predicates.add(criteriaBuilder.equal(root.get("address"), student.getAddress()));
            }
            predicates.add(criteriaBuilder.equal(root.get("state"), 0));
            return criteriaQuery.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
        }, request);
    }
}
